package com.psp.park.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.northend.util.AppTextUtil;
import com.psp.park.cache.dao.UserCacheDao;
import com.psp.park.controller.res.BaseResult;
import com.psp.park.controller.res.ObjectResult;
import com.psp.park.controller.res.bean.RUserBean;
import com.psp.park.controller.springmvc.req.CheckLoginUserParam;
import com.psp.park.controller.springmvc.req.CheckRegUserParam;
import com.psp.park.controller.springmvc.req.GetUserParam;
import com.psp.park.controller.springmvc.req.LoginParam;
import com.psp.park.model.ParkUserBean;
import com.psp.park.service.UserService;
import com.psp.park.service.exception.ServiceException;

@Component
public class UserController {
	Logger logger = Logger.getLogger(UserController.class);
	@Autowired
	UserService userServiceImpl;
	@Autowired
	UserCacheDao userCacheImpl;

	/**
	 * 登录
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ObjectResult<RUserBean> login(LoginParam param, HttpServletRequest request, HttpServletResponse response) {
		ObjectResult<RUserBean> result = new ObjectResult<RUserBean>();
		try {
			String userName = param.getUserName();
			String pwd = param.getPassword();
			String vcode = param.getImgCode();
			String device = param.getDevice();
			String sessionId = request.getSession().getId();
			String ip = AppTextUtil.getIpAddr(request);
			ParkUserBean user = userServiceImpl.login(sessionId, userName, pwd, vcode, device, ip);
			logger.info("login user is:" + user);
			if (user != null) {
				RUserBean data = new RUserBean();
				data.setUserName(user.getNickName());
				data.setAccount(user.getNickName());
				data.setHeadIcon(user.getHeadIcon());
				result.setToken(sessionId);
				result.setData(data);
				result.setFlag(true);
			}
		} catch (ServiceException e) {
			result.setFlag(false);
			result.setRescode(e.getServiceCode());
			result.setMsg(e.getServiceMsg());
		}
		return result;
	}

	/**
	 * 检查注册用户
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult checkRegUser(CheckRegUserParam param, HttpServletRequest request, HttpServletResponse response) {
		BaseResult result = new BaseResult();
		try {
			String username = param.getUserName();
			ParkUserBean user = userServiceImpl.existUser(1, username);
			if (user != null) {
				result.setFlag(true);
			}
		} catch (ServiceException e) {
			result.setFlag(false);
			result.setMsg(e.getServiceMsg());
		}
		return result;
	}

	/**
	 * 检查登录用户
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult checkLoginUser(CheckLoginUserParam param, HttpServletRequest request,
			HttpServletResponse response) {
		BaseResult result = new BaseResult();
		try {
			String username = param.getUserName();
			ParkUserBean user = userServiceImpl.existUser(2, username);
			if (user != null) {
				result.setFlag(true);
			}
		} catch (ServiceException e) {
			result.setFlag(false);
			result.setMsg(e.getServiceMsg());
		}
		return result;
	}

	/**
	 * 获取用户信息
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ObjectResult<RUserBean> getUser(GetUserParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ObjectResult<RUserBean> result = new ObjectResult<RUserBean>();
		String token = (String) request.getAttribute("token");
		String puid = (String) request.getAttribute("userId");
		try {
			ParkUserBean user = userServiceImpl.getUser(puid);
			logger.info("login user is:" + user);
			if (user != null) {
				RUserBean data = new RUserBean();
				data.setUserName(user.getNickName());
				data.setAccount(user.getNickName());
				data.setHeadIcon(user.getHeadIcon());
				data.setPark(user.getPark());
				result.setData(data);
				result.setFlag(true);
			}
		} catch (ServiceException e) {
			result.setFlag(false);
			result.setMsg(e.getServiceMsg());
		}
		result.setToken(token);
		return result;
	}

}
